package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.IWxMaterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WxDriverRecord;
import com.ruoyi.system.service.IWxDriverRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.validation.constraints.Size;

/**
 * recordController
 *
 * @author ruoyi
 * @date 2021-07-27
 */
@Controller
@RequestMapping("/system/record")
public class WxDriverRecordController extends BaseController {
    private String prefix = "system/record";

    @Autowired
    private IWxDriverRecordService wxDriverRecordService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IWxMaterService wxMaterService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record(ModelMap mmp) {

        mmp.put("deptTree", sysDeptService.deptTree(101L));
        return prefix + "/record";
    }

    /**
     * 查询record列表
     */
    @RequiresPermissions("system:record:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(WxDriverRecord wxDriverRecord) {

        Map<Object, Object> resMap = initRes(wxDriverRecord);

        startPage();
        List<WxDriverRecord> list = wxDriverRecordService.selectWxDriverRecordLists(resMap);
        return getDataTable(list);
    }

    private Map<Object, Object> initRes(WxDriverRecord wxDriverRecord) {
        SysUser currentUser = ShiroUtils.getSysUser();
        String phonenumber = currentUser.getPhonenumber();
        // 根据用户手机号，获取项目权限
        List<SysDept> sysDepts = wxMaterService.selectDeptsByMaterPhone(phonenumber);
        if (StringUtils.isNotEmpty(sysDepts)) {
            List<Long> deptList = new ArrayList<>();
            for (SysDept dept : sysDepts) {
                deptList.add(dept.getDeptId());
            }
            wxDriverRecord.setDepts(deptList);
        }
        Map<Object, Object> resMap = new HashMap<>();
        resMap.put("programName", wxDriverRecord.getProgramName());
        resMap.put("supplierName", wxDriverRecord.getSupplierName());
        resMap.put("driverName", wxDriverRecord.getDriverName());
        resMap.put("licensePlate", wxDriverRecord.getLicensePlate());
        resMap.put("remark", wxDriverRecord.getRemark());
        resMap.put("beginDayTime", wxDriverRecord.getParams().get("beginDayTime"));
        resMap.put("endDayTime", wxDriverRecord.getParams().get("endDayTime"));

        if (currentUser.getLoginName().equals("admin") || currentUser.getDeptId() == 101) {
            resMap.put("depts", null);
        } else {
            resMap.put("depts", wxDriverRecord.getDepts());
        }


        return resMap;
    }

    /**
     * 导出record列表
     */
    @RequiresPermissions("system:record:export")
    @Log(title = "record", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WxDriverRecord wxDriverRecord) {
        Map<Object, Object> resMap = initRes(wxDriverRecord);
        List<WxDriverRecord> list = wxDriverRecordService.selectWxDriverRecordListForExport(resMap);
        if (StringUtils.isNotEmpty(list)) {
            for (WxDriverRecord record : list) {
                record.setClockDate(record.getCreateTimes().substring(0, 10));
                record.setClockTime(record.getCreateTimes().substring(11));
            }
        }

        ExcelUtil<WxDriverRecord> util = new ExcelUtil<WxDriverRecord>(WxDriverRecord.class);
        return util.exportExcel(list, "record数据");
    }

    /**
     * 新增record
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存record
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "record", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WxDriverRecord wxDriverRecord) {
        return toAjax(wxDriverRecordService.insertWxDriverRecord(wxDriverRecord));
    }

    /**
     * 修改record
     */
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap) {
        WxDriverRecord wxDriverRecord = wxDriverRecordService.selectWxDriverRecordById(recordId);
        mmap.put("workContents", sysDictDataService.selectDictDataByType("work_content"));
        mmap.put("remarks", sysDictDataService.selectUnloadingByDictType(wxDriverRecord.getWorkContent()));
        mmap.put("wxDriverRecord", wxDriverRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存record
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "record", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WxDriverRecord wxDriverRecord) {
        return toAjax(wxDriverRecordService.updateWxDriverRecord(wxDriverRecord));
    }

    /**
     * 删除record
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "record", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wxDriverRecordService.deleteWxDriverRecordByIds(ids));
    }

    @RequestMapping("/selectUnloadingByDictType")
    @ResponseBody
    public List<Map> selectUnloadingByDictType(String workContent) {
        return sysDictDataService.selectUnloadingByDictType(workContent);
    }

}
